package com.agileboot.admin.controller.danger;

import com.agileboot.admin.customize.aop.accessLog.AccessLog;
import com.agileboot.common.core.base.BaseController;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.common.enums.common.BusinessTypeEnum;
import com.agileboot.common.utils.poi.CustomExcelUtil;
import com.agileboot.domain.common.command.BulkOperationCommand;

import com.agileboot.domain.system.danger.DangerSourceApplicationService;
import com.agileboot.domain.system.danger.command.AddDangerSourceCommand;
import com.agileboot.domain.system.danger.command.UpdateDangerSourceCommand;
import com.agileboot.domain.system.danger.dto.DangerSourceDTO;
import com.agileboot.domain.system.danger.query.DangerSourceQuery;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 危险源 API
 * @author [your-name]
 */
@Tag(name = "危险源API", description = "危险源相关的增删查改")
@RestController
@RequestMapping("/airport/danger-source")
@Validated
@RequiredArgsConstructor
public class DangerSourceController extends BaseController {

    private final DangerSourceApplicationService dangerSourceApplicationService;

    /**
     * 获取危险源列表
     */
    @Operation(summary = "危险源列表")
//    @PreAuthorize("@permission.has('system:danger-source:list')")
    @GetMapping("/list")
    public ResponseDTO<PageDTO<DangerSourceDTO>> list(DangerSourceQuery query) {
        PageDTO<DangerSourceDTO> pageDTO = dangerSourceApplicationService.getDangerSourceList(query);
        return ResponseDTO.ok(pageDTO);
    }

    /**
     * 导出危险源列表
     */
    @Operation(summary = "导出危险源")
    @AccessLog(title = "危险源管理", businessType = BusinessTypeEnum.EXPORT)
//    @PreAuthorize("@permission.has('system:danger-source:export')")
    @GetMapping("/excel")
    public void export(HttpServletResponse response, DangerSourceQuery query) {
        List<DangerSourceDTO> all = dangerSourceApplicationService.getDangerSourceListAll(query);
        CustomExcelUtil.writeToResponse(all, DangerSourceDTO.class, response);
    }

    /**
     * 获取危险源详情
     */
    @Operation(summary = "危险源详情")
//    @PreAuthorize("@permission.has('system:danger-source:query')")
    @GetMapping("/{id}")
    public ResponseDTO<DangerSourceDTO> getInfo(@PathVariable Long id) {
        DangerSourceDTO dto = dangerSourceApplicationService.getDangerSourceInfo(id);
        return ResponseDTO.ok(dto);
    }

    /**
     * 新增危险源
     */
    @Operation(summary = "新增危险源")
//    @PreAuthorize("@permission.has('system:danger-source:add')")
    @AccessLog(title = "危险源管理", businessType = BusinessTypeEnum.ADD)
    @PostMapping
    public ResponseDTO<Void> add(@RequestBody AddDangerSourceCommand command) {
        dangerSourceApplicationService.addDangerSource(command);
        return ResponseDTO.ok();
    }

    /**
     * 修改危险源
     */
    @Operation(summary = "修改危险源")
//    @PreAuthorize("@permission.has('system:danger-source:edit')")
    @AccessLog(title = "危险源管理", businessType = BusinessTypeEnum.MODIFY)
    @PutMapping
    public ResponseDTO<Void> edit(@RequestBody UpdateDangerSourceCommand command) {
        dangerSourceApplicationService.updateDangerSource(command);
        return ResponseDTO.ok();
    }

    /**
     * 删除危险源
     */
    @Operation(summary = "删除危险源")
//    @PreAuthorize("@permission.has('system:danger-source:remove')")
    @AccessLog(title = "危险源管理", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping
    public ResponseDTO<Void> remove(@RequestParam @NotNull @NotEmpty List<Long> ids) {
        dangerSourceApplicationService.deleteDangerSource(new BulkOperationCommand<>(ids));
        return ResponseDTO.ok();
    }

    /**
     * HTML格式展示所有危险源
     */
    @Operation(summary = "以HTML格式获取全部危险源")
    @GetMapping("/getAll")
    @ResponseBody
    public String getAllDangerSourcesAsHtml() {
        List<DangerSourceDTO> list = dangerSourceApplicationService.getDangerSourceListAll(new DangerSourceQuery());

        StringBuilder html = new StringBuilder();
        html.append("<html><body>");
        html.append("<h2>危险源列表</h2>");
        for (DangerSourceDTO dto : list) {
            html.append("<div><strong>危险源名称:</strong> ").append(dto.getDangerSourceName()).append("</div>");
            html.append("<div><strong>危险源编号:</strong> ").append(dto.getDangerSourceCode()).append("</div>");
            html.append("<hr>");
        }
        html.append("</body></html>");

        return html.toString();
    }
}
